// pages/Details/Details.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tapMe1:false,
    isSearch:true,
    isCear:false,
    val:'',
    Search_del: false,
    no_data: false,
    list: ''
  },

  getData: function(){
    var that = this;
    wx.request({
      url: `https://199shq.com/api/tproduct/search?name=`,
      method: 'GET',
      header: {
        'Content-Type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function(res) {
        if (res.data.length == 0) {
          console.log("该分类暂时没有商品");
          that.setData({
            no_data: true
          })
        }else{
          that.setData({
            no_data: false
          })
        }
        console.log(res.data);
        var list = res.data;
        if (list == null) {
          var toastText = '数据获取失败';
          wx.showToast({
            title: 'toastText',
            icon: '',
            duration: 2000
          });
        } else {
          that.setData({
            list: list
          })
        }
      }
    })
  },

  //搜索事件定义
Search: function(event){
  console.log('回车键被按下了')
  const value = event.detail.value;
  console.log('输入内容：', value);
  var that = this;
    wx.request({
      url: `https://199shq.com/api/tproduct/search?name=${value}`,
      method: 'GET',
      header: {
        'Content-Type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function(res) {
        if (res.data.length == 0) {
          console.log("该分类暂时没有商品");
          that.setData({
            no_data: true
          })
        }else{
          that.setData({
            no_data: false
          })
        }
        console.log(res.data);
        var list = res.data;
        if (list == null) {
          var toastText = '数据获取失败';
          wx.showToast({
            title: 'toastText',
            icon: '',
            duration: 2000
          });
        } else {
          that.setData({
            list: list
          })
        }
      }
    })
},

//点击获取input内容
seldata: function(event){
  const value = event.detail.value;
  console.log(value);
    this.setData({
      val: value,
      Search_del: true
    });
    if (value == '') {
      this.setData({
        Search_del: false
      });
    }
},

//点击清空搜索框
Search_del: function() {
  this.setData({
    val: '',
    Search_del: false
  });
},

//点击触发搜索
onInput: function(){ 
  
  const value = this.data.val;
  console.log('输入内容：', value);
  
  var that = this;
    wx.request({
      url: `https://199shq.com/api/tproduct/search?name=${value}`,
      method: 'GET',
      header: {
        'Content-Type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function(res) {
        if (res.data.length == 0) {
          console.log("该分类暂时没有商品");
          that.setData({
            no_data: true
          })
        }else{
          that.setData({
            no_data: false
          })
        }
        console.log(res.data);
        var list = res.data;
        if (list == null) {
          var toastText = '数据获取失败';
          wx.showToast({
            title: 'toastText',
            icon: '',
            duration: 2000
          });
        } else {
          that.setData({
            list: list
          })
        }
      }
    })
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log("11111")
    var that = this;
    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})